/*
 * KKCallback.cpp
 *
 *  Created on: 2013-9-15
 *      Author: wanghua
 */

#include "KKCallback.h"

KKCallback::KKCallback(CCObject* target, SEL_CallFuncO callback,
		CCObject* args) {
	this->args = NULL;
	this->target = NULL;
	setTarget(target);
	this->callback = callback;
	setArgs(args);
}

KKCallback::~KKCallback() {

	if (target) {
		target->release();
	}

	if (args) {
		args->release();
	}

	target = NULL;
	callback = NULL;
	args = NULL;
}

void KKCallback::call(CCObject* source) {
	if (target && callback) {
		(target->*callback)(args);
	} else {
		CCLog("callback target or function is null\n");
	}
}

void KKCallback::setTarget(CCObject* target) {

	if (this->target != target) {

		if (this->target) {
			this->target->release();
		}

		this->target = target;

		if (this->target) {
			this->target->retain();
		}

	}

}

CCObject* KKCallback::getTarget() {
	return this->args;
}

void KKCallback::setArgs(CCObject* args) {

	if (this->args != args) {

		if (this->args) {
			this->args->release();
		}

		this->args = args;

		if (this->args) {
			this->args->retain();
		}

	}
}

CCObject* KKCallback::getArgs() {
	return this->args;
}
